import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/material/input_decorator.dart';
import 'package:flutter/src/services/text_formatter.dart';
import 'package:hygf/res/HDColors.dart';


import '../../../config/StyleConfig.dart';
import '../bean/AbstactCardContainerBean.dart';
import 'CardContainerHorizontal.dart';

class FieldEditor extends FormField<String>
    implements CardContainerBean, TextFieldBean {
  @override
  bool? autoFocus;

  @override
  bool? autoValidate;

  @override
  Widget? content;

  @override
  TextAlign contentTextAlign;

  @override
  TextStyle contentTextStyle;

  @override
  TextEditingController? controller;

  @override
  InputDecoration? decoration;

  @override
  String? errorText;

  @override
  String? hintText;

  TextStyle? hintStyle;

  @override
  List<TextInputFormatter>? inputFormatterList;

  @override
  bool? isRequired;

  @override
  TextInputType? keyboardType;

  @override
  String? label;

  @override
  String? hintLabel;

  @override
  MainAxisAlignment? labelAlign;

  @override
  double? labelIconSize;

  @override
  TextStyle? labelStyle;

  @override
  Widget? labelSuffix;

  @override
  double? labelWitch;

  @override
  Icon? leftIcon;

  @override
  int? maxLength;

  @override
  int? maxLines;

  @override
  int? minLines;

  @override
  ValueChanged<String>? onChanged;

  @override
  String? prefixText;

  @override
  bool? readOnly;

  @override
  Icon? rightIcon;

  @override
  bool? showLine;

  @override
  bool? visible;

  @override
  String? initialValue;

  @override
  FormFieldSetter<String>? onSaved;

  @override
  FormFieldValidator<String>? validator;

  FieldEditor(
      {Key? key,
      this.onSaved,
      this.validator,
      this.initialValue,
      this.label = 'Label',
      this.visible = true,
      this.onChanged,
      this.leftIcon,
      this.rightIcon,
      this.hintText,
      this.hintStyle,
      this.prefixText,
      this.decoration,
      this.keyboardType,
      this.autoFocus = false,
      this.readOnly = false,
      this.maxLines = 1,
      this.minLines,
      this.maxLength,
      this.inputFormatterList,
      this.isRequired,
      this.contentTextAlign = TextAlign.start,
      this.contentTextStyle = const TextStyle(
          fontSize: StyleConfig.fontContentSize16, fontWeight: FontWeight.normal),
      this.controller,
      this.labelSuffix,
      this.autoValidate,
      this.showLine})
      : super(
            key: key,
            initialValue: initialValue,
            onSaved: onSaved,
            validator: validator,
            //autovalidateMode:autoValidate,
            builder: (field) {
              return (field as _FieldEditorState)._build(field.context);
            });

  @override
  _FieldEditorState createState() => _FieldEditorState();
}

class _FieldEditorState extends FormFieldState<String> {
  TextEditingController? _controller;

  TextEditingController? get _effectiveController =>
      widget.controller ?? _controller;

  @override
  FieldEditor get widget => super.widget as FieldEditor;

  @override
  void initState() {
    super.initState();
    if (widget.controller == null) {
      _controller = TextEditingController(text: widget.initialValue);
    } else {
      widget.controller?.addListener(_handleControllerChanged);
    }
  }

  @override
  void reset() {
    super.reset();
    setState(() {
      _effectiveController?.text = widget.initialValue!;
    });
  }

  @override
  void dispose() {
    widget.controller?.removeListener(_handleControllerChanged);
    super.dispose();
  }

  @override
  void didUpdateWidget(FieldEditor oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.controller != oldWidget.controller) {
      oldWidget.controller?.removeListener(_handleControllerChanged);
      widget.controller?.addListener(_handleControllerChanged);
      if (oldWidget.controller != null && widget.controller == null) {
        _controller =
            TextEditingController.fromValue(oldWidget.controller?.value);
      }
      if (widget.controller != null) {
        setValue(widget.controller?.text);
        if (oldWidget.controller == null) _controller = null;
      }
    } else {
      //表示相同的控件
      //if(this.value!=widget.initialValue){
      if (_effectiveController?.text != widget.initialValue) {
        setValue(widget.initialValue ?? "");
      }
      _effectiveController?.text = widget.initialValue ?? "";

      //}
    }
  }

  void _handleControllerChanged() {
    if (_effectiveController?.text != value) {
      didChange(_effectiveController?.text);
    }
  }

  void _handleOnChanged(String value) {
    // if (this.value != value) {
      didChange(value);
      if (widget.onChanged != null) {
        widget.onChanged!(value);
      }
    // }
  }

  Widget _build(BuildContext context) {
    // make local mutable copies of values and options
    return CardContainerHorizontal(
      label: widget.label,
      visible: widget.visible,
      leftIcon: widget.leftIcon,
      rightIcon: widget.rightIcon,
      errorText: errorText,
      isRequired: widget.isRequired,
      showLine: widget.showLine,
      content: TextField(
        cursorColor: HDColors.green,
        // Theme.of(context).cursorColor,
        cursorWidth: 2.0,
        controller: _effectiveController,
        onChanged: _handleOnChanged,

        decoration: widget.decoration ??
            InputDecoration(
              border: InputBorder.none,
              errorText: errorText,
              prefixText: widget.prefixText,
              hintText: (widget.readOnly ?? false) ? "" : widget.hintText,
              hintStyle: widget.hintStyle,
              contentPadding: const EdgeInsets.only(top: 0, bottom: 0),
              isDense: true,
            ),
        readOnly: widget.readOnly ?? false,
        maxLines: widget.maxLines,
        minLines: widget.minLines,
        autofocus: widget.autoFocus ?? false,
        style: widget.contentTextStyle,
        textAlign: widget.contentTextAlign,
        keyboardType: widget.keyboardType,
        maxLength: widget.maxLength,
        inputFormatters: widget.inputFormatterList ??
            [
              // if we don't want the counter, use this maxLength instead
              LengthLimitingTextInputFormatter(widget.maxLength)
            ],
      ),
//        pickerIcon: Icons.arrow_drop_down,
    );
  }
}
